package com.itggm.controller;


import com.itggm.Result.ResponseResult;
import com.itggm.dto.SchedulePageDTO;
import com.itggm.exception.BusinessException;
import com.itggm.pojo.Schedule;
import com.itggm.service.ScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: ggm
 * @date: 2025/1/2
 */
@Slf4j
@RestController
@RequestMapping("schedule")
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;

    @PostMapping("page")
    public ResponseResult schedulePage(@RequestBody SchedulePageDTO schedulePageDTO) {
        log.info("分页查询:{}", schedulePageDTO);
        return scheduleService.schedulePage(schedulePageDTO);
    }


    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdateSchedule(@RequestBody Schedule schedule) {
        log.info("添加或修改:{}", schedule);
        try {
            scheduleService.saveOrUpdate(schedule);
            return ResponseResult.success();
        } catch (DuplicateKeyException e) {
            throw new BusinessException("课程编号重复");
        }

    }

    @DeleteMapping("delete/{id}")
    public ResponseResult deleteSchedule(@PathVariable("id") Integer id) {
        log.info("删除课程信息{}", id);
        scheduleService.removeById(id);
        return ResponseResult.success();
    }

}
